package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;

import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.util.ZStringHelp;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.view.window.loading.WindowLoading;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;
import com.zlzxm.shoesmarket.view.window.wheelpick.OnOptionSelectListener;
import com.zlzxm.shoesmarket.view.window.wheelpick.WindowWheelPick;
import com.zlzxm.shoesmarket.viewmodel.VMAddRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zlz
 * on  4/14/21
 * <br>
 *     新增仓库
 */
public class ACAddRepository extends BaseActivity implements TextWatcher , View.OnClickListener {

    public static void start(Context context) {
        Intent starter = new Intent(context, ACAddRepository.class);
        context.startActivity(starter);
    }

    private VMAddRepository vmAddRepository;

    private EditText etName,etBrokerage;

    private TextView tvSure, tvType;



    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_update_repository);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("新增仓库");
        simpleHead.setOnItemclicklistener(this::finish);

        etName  = ZViewHelp.findById(this,R.id.etName);
        etBrokerage  = ZViewHelp.findById(this,R.id.etBrokerage);
        tvType = ZViewHelp.setOnClickListener(this,R.id.tvType,this);
        tvSure  = ZViewHelp.setOnClickListener(this,R.id.tvSure,this);

        etName.addTextChangedListener(this);
        etBrokerage.addTextChangedListener(this);
        tvType.addTextChangedListener(this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmAddRepository = new ViewModelProvider(this).get(VMAddRepository.class);

        vmAddRepository.liveToast.observe(this,this::succeedToast);

        vmAddRepository.liveSelectType.observe(this,it -> tvType.setText(it.getName()));

        vmAddRepository.getLiveErrorToast().observe(this,this::errorToast);

        vmAddRepository.getLiveSucceedToast().observe(this,this::succeedToast);

    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {

        String name = etName.getText().toString();
        String brokerage = etBrokerage.getText().toString();
        String type = tvType.getText().toString();

        if(ZStringHelp.isEmpty(name,brokerage,type)){

            tvSure.setEnabled(false);

        }else {

            tvSure.setEnabled(true);
        }
    }

    @Override
    public void onClick(View view) {

        ZViewHelp.hideKeyBoard(getWindow(),this);

        if(view.getId() == R.id.tvType){


            showTypeWindow();

        }else if(view.getId() == R.id.tvSure){


            vmAddRepository.addRepository(WindowLoading.getInstance(),getSupportFragmentManager()
            ,etBrokerage.getText().toString(),etName.getText().toString());

        }

    }


    //<editor-fold defaultState="collapsed" desc="私有方法">



    private void showTypeWindow(){


        List<EntityOption> options = new ArrayList<>();

        for (Constant.RepositoryType type : Constant.RepositoryType.values()) {

            EntityOption entityOption = new EntityOption(type.getType()+"",type.getValue());

            options.add(entityOption);

        }


        WindowWheelPick.getInstance()
                .setOptions(options)
                .setOnOptionSelectListener(new OnOptionSelectListener() {
                    @Override
                    public void onSelected(EntityOption entityOption) {

                        vmAddRepository.liveSelectType.setValue(entityOption);

                    }
                })
                .show(getSupportFragmentManager(),"");


    }


    // </editor-fold>
}
